const app = getApp()
var isclickNav = false
var rightTitle = ''
var leftTitle= ''
var rightTitleColor
var leftTitleColor
var leftBorderColor
var rightBorderColor
var isLeftBtnHidden = false 
var isRightBtnHidden = false 
// 分页
var bingPage = 1
var phoneNum = ''
// 按钮的状态(取消订单: 0,去支付: 1,申请退款: 2,去评价: 3,查看退款详情: 4)
var btnState = 999
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: '',
    iconBgImage: '/images/order_more.png',
    arrowImage: '/images/order_arrow.png',
    noDataImage: '/images/order_blank.png',
    noLoginImage: '/images/noLogin.png',
    listArr: [],
    // 列表的总高度
    listHeight: 0,
    // 待确定的数量
    sureCount: 0,
    // 待付款的数量
    payCount: 0,
    // 待入住的数量
    checkInCount: 0,
    // 待评价的数量
    evaluateCount: 0,
    // 左侧按钮的状态
    leftBtnStateArr: [],
    // 左侧按钮的标题
    leftBtnTitle: [],
    // 右侧按钮的标题
    rightTitleArr: [],
    // 右侧按钮标题的颜色
    rightTitleColorArr: [],
    // 右侧按钮边框的颜色
    rightBorderColorArr: [],
    // 判断是否登录
    islogin: false,
  },
  // 加载数据
  loadData(username, orderType, bingPage) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.baseNet + 'orderInfo/queryOrderInfo?',
      data: {
        username: username,
        orderType: orderType,
        bingPage: bingPage
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        wx.hideLoading()
        console.log(res.data)
        if (res.data.messcode == '001') {
          // 有数据
          var height = res.data.orderList.length * 365
          that.setData({
            listArr: res.data.orderList,
            listHeight: height,
          })
          that.judgeState()
        }
      },
    })
  },
  // 右侧按钮的点击事件
  rightBtnClick: function(e) {
    var that = this
    var model = this.data.listArr[e.currentTarget.dataset.id]
    that.judgeRightTtnState(model.state)
    console.log(btnState)
    switch (btnState) {
      case 0: 
        // 取消订单
        wx.navigateTo({
          url: '../cancelOrder/cancelOrder?soiId=' + model.soiId,
        })
        break;
      case 1:
        // 去支付
        wx.navigateTo({
          url: '../toPayfor/toPayfor?soiId='+model.soiId,
        })
        break;
      case 2:
        // 申请退款
        wx.navigateTo({
          url: '../toPayfor/toPayfor?soiId=' + model.soiId,
        })
        break;
      case 3:
        // 去评价
        break;
      case 4:
        // 查看退款详情
        break;
    }
  },
  // 左侧按钮的点击事件
  leftBtnClick: function(e) {
    // 取消订单
    var model = this.data.listArr[e.currentTarget.dataset.id]
    wx.navigateTo({
      url: '../cancelOrder/cancelOrder?soiId=' + model.soiId,
    })
  },
  // 首次进入计算确定\付款\入住\评价的数量
  loadDataCalculate(username, orderType, bingPage) {
    var that = this;
    wx.request({
      url: app.baseNet + 'orderInfo/queryOrderInfo?',
      data: {
        username: username,
        orderType: orderType,
        bingPage: bingPage
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        var count = res.data.orderList.length
        switch (orderType) {
          case 2:
            that.setData({
              sureCount: count
            })
          break
          case 3:
            that.setData({
              payCount: count
            })
          break
          case 4: 
            that.setData({
              checkInCount: count
            })
          break
          case 5:
            that.setData({
              evaluateCount: count
            })
          break
        }
      },
    })
  },
  /*** 滑动切换tab***/
  bindChange: function (e) {

    var that = this;
    if (isclickNav == false) {
      this.loadData(phoneNum, e.detail.current + 1, bingPage)
    } else {
      isclickNav = false
    }
    that.setData({ 
      currentTab: e.detail.current 
    });
  },
  /*** 点击tab切换***/
  swichNav: function (e) {
    isclickNav = true 
    var that = this;
    this.loadData(phoneNum, parseInt(e.target.dataset.current) + 1, bingPage)
    that.setData({
      currentTab: e.target.dataset.current
    });
  },

  judgeState() {
    this.data.leftBtnStateArr = []
    this.data.rightTitleArr = []
    this.data.rightTitleColorArr = []
    this.data.rightBorderColorArr = []
    for (var i = 0; i < this.data.listArr.length; i++) {
      let state = this.data.listArr[i].state
      switch (state) {
        case 0:
          isLeftBtnHidden = true 
          isRightBtnHidden = false 
          leftTitle = ''
          rightTitle = '取消订单'
          leftTitleColor = ''
          rightTitleColor = '#646464'
          leftBorderColor = ''
          rightBorderColor = '#969696'
          btnState = 0
          break;
        case 1:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = '取消订单'
          rightTitle = '去支付'
          leftTitleColor = ''
          rightTitleColor = '#01E2E6'
          leftBorderColor = ''
          rightBorderColor = '#01E2E6'
          btnState = 1
          break;
        case 2:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = ''
          rightTitle = '申请退款'
          leftTitleColor = ''
          rightTitleColor = '#01E2E6'
          leftBorderColor = ''
          rightBorderColor = '#01E2E6'
          btnState = 2
          break;
        case 3:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = ''
          rightTitle = '申请退款'
          leftTitleColor = ''
          rightTitleColor = '#01E2E6'
          leftBorderColor = ''
          rightBorderColor = '#01E2E6'
          btnState = 2
          break;
        case 4:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = ''
          rightTitle = '去评价'
          leftTitleColor = ''
          rightTitleColor = '#01E2E6'
          leftBorderColor = ''
          rightBorderColor = '#01E2E6'
          btnState = 3
          break;
        case 5:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = ''
          rightTitle = '已评价'
          leftTitleColor = ''
          rightTitleColor = '#cccccc'
          leftBorderColor = ''
          rightBorderColor = '#cccccc'
          break;
        case 6:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = ''
          rightTitle = '已取消'
          leftTitleColor = ''
          rightTitleColor = '#cccccc'
          leftBorderColor = ''
          rightBorderColor = '#cccccc'
          break;
        case 7:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = ''
          rightTitle = '退款成功'
          leftTitleColor = ''
          rightTitleColor = '#cccccc'
          leftBorderColor = ''
          rightBorderColor = '#cccccc'
          break;
        case 8:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = ''
          rightTitle = '查看退款详情'
          leftTitleColor = ''
          rightTitleColor = '#646464'
          leftBorderColor = ''
          rightBorderColor = '#969696'
          btnState = 4
          break;
        case 9:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = ''
          rightTitle = '查看退款详情'
          leftTitleColor = ''
          rightTitleColor = '#646464'
          leftBorderColor = ''
          rightBorderColor = '#969696'
          btnState = 4
          break;
        case 10:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = ''
          rightTitle = '已失效'
          leftTitleColor = ''
          rightTitleColor = '#cccccc'
          leftBorderColor = ''
          rightBorderColor = '#cccccc'
          break;
        case 11:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = ''
          rightTitle = '退款成功'
          leftTitleColor = ''
          rightTitleColor = '#cccccc'
          leftBorderColor = ''
          rightBorderColor = '#cccccc'
          break;
        case 12:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = ''
          rightTitle = '已失效'
          leftTitleColor = ''
          rightTitleColor = '#cccccc'
          leftBorderColor = ''
          rightBorderColor = '#cccccc'
          break;
        case 13:
          isLeftBtnHidden = true
          isRightBtnHidden = false
          leftTitle = ''
          rightTitle = '申请退款'
          leftTitleColor = ''
          rightTitleColor = '#ff6b6b'
          leftBorderColor = ''
          rightBorderColor = '#ff6b6b'
          btnState = 2
          break;
      }
      this.data.leftBtnStateArr.push(isLeftBtnHidden)
      this.data.rightTitleArr.push(rightTitle)
      this.data.rightTitleColorArr.push(rightTitleColor)
      this.data.rightBorderColorArr.push(rightBorderColor)
      console.log(this.data.rightBorderColorArr)
      this.setData({
        leftBtnStateArr: this.data.leftBtnStateArr,
        rightTitleArr: this.data.rightTitleArr,
        rightTitleColorArr: this.data.rightTitleColorArr,
        rightBorderColorArr: this.data.rightBorderColorArr,
      });
    }
  },
  // 判断右侧按钮的点击样式
  judgeRightTtnState(state) {
    switch (state) {
      case 0:
        btnState = 0
        break;
      case 1:
        btnState = 1
        break;
      case 2:
        btnState = 2
        break;
      case 3:
        btnState = 2
        break;
      case 4:
        btnState = 3
        break;
      case 5:
        break;
      case 6:
        break;
      case 7:
        break;
      case 8:
        btnState = 4
        break;
      case 9:
        btnState = 4
        break;
      case 10:
        break;
      case 11:
        break;
      case 12:
        break;
      case 13:
        btnState = 2
        break;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.loadData(15075182791, 1, 1)
    // this.loadDataCalculate(15075182791, 2, 1)
    // this.loadDataCalculate(15075182791, 3, 1)
    // this.loadDataCalculate(15075182791, 4, 1)
    // this.loadDataCalculate(15075182791, 5, 1)
  },

  // 下拉刷新
  onPullDownRefresh: function () {
    console.log('上垃圾啊在')
    wx.showNavigationBarLoading() //在标题栏中显示加载

    //模拟加载
    setTimeout(function () {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 4000);
  },
  loadMoreData: function () {
    // 我们用total和count来控制分页，total代表已请求数据的总数，count代表每次请求的个数。
    // 上拉时需把total在原来的基础上加上count，代表从count条后的数据开始请求。
    console.log('上垃圾啊在')
    bingPage += 1
    // 网络请求
    this.loadData();
  },

  // 点击登录注册
  loginBtnClick: function() {
    // 判断是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已授权
          var isFirstLogin = wx.getStorageSync('isNofirstLogin')
          if (isFirstLogin) {
            // 不是第一次登陆
            wx.navigateTo({
              url: '../fastLoginAndRegister/fastLoginAndRegister',
            })
          } else {
            // 是第一次登陆
            wx.navigateTo({
              url: '../loginAndRegister/loginAndRegister?jumpType=1',
            })
          }
        } else {
          // 未授权
          wx.navigateTo({
            url: '../loginAndRegister/loginAndRegister?jumpType=1',
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var phoneNumber = wx.getStorageSync('phoneNumber')
    phoneNum = phoneNumber
    if (phoneNumber != null && phoneNumber != '') {
      // 登录
      that.setData({
        islogin: true
      })
      this.loadData(phoneNum, 1, bingPage)
      this.loadDataCalculate(phoneNum, 2, bingPage)
      this.loadDataCalculate(phoneNum, 3, bingPage)
      this.loadDataCalculate(phoneNum, 4, bingPage)
      this.loadDataCalculate(phoneNum, 5, bingPage)
    } else {
      // 未登录
      that.setData({
        islogin: false 
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})